package com.zzyl.mapper;

import com.zzyl.entity.Reservation;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.TimeCountVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ReservationMapper {

    //查询指定用户在某个时间段内的取消次数
    @Select("select count(*) from reservation where update_time between #{begin} and #{end} and status = 2 and update_by = #{userId}")
    Integer countCancelledReservationsWithinTimeRange(LocalDateTime begin, LocalDateTime end, Long userId);

    //查询每个时间段剩余预约次数
    @Select("SELECT time, 6 - COUNT(*) AS count FROM reservation WHERE time BETWEEN #{begin} AND #{end} and status != 2 GROUP BY time")
    List<TimeCountVo> countReservationsForEachTimeWithinTimeRange(LocalDateTime begin, LocalDateTime end);

    //添加预约
    @Insert("insert into reservation(name, mobile, time, visitor, type, status, create_by, remark, create_time)\n" +
            "values (#{name}, #{mobile}, #{time}, #{visitor}, #{type}, #{status}, #{createBy}, #{remark}, #{createTime})")
    int save(Reservation reservation);

    //查询预约
    List<Reservation> findList(Long userId,  Integer status);

    //更新预约
    void update(Reservation reservation);
}
